package org.szgb.console.sys.line.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysLine extends BeanEntity<SysLine> {
    @Serial
    private static final long serialVersionUID = 1L;
    private String name;

    public SysLine() {
    }

    public SysLine(String id) {
        this.setId(id);
    }

    public SysLine(SysLineFindDto sysLineFindDto){
        this.setId(sysLineFindDto.getId());
        this.setName(sysLineFindDto.getName());
        this.setPageDto(sysLineFindDto.getPageDto());
    }

    public SysLine(SysLineSaveDto sysLineSaveDto){
        this.setId(sysLineSaveDto.id);
        this.setName(sysLineSaveDto.name);
    }

    public SysLineView toSysLineView() {
        return new SysLineView(this);
    }

    @Data
    @Schema(title = "线路信息", description = "线路信息")
    public static class SysLineView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysLineView(SysLine sysLine) {
            this.id = sysLine.getId();
            this.name = sysLine.getName();
            this.createdAt = sysLine.getCreatedAt();
            this.updatedAt = sysLine.getUpdatedAt();
            if (sysLine.getCreatedBy() != null) {
                this.createdById = sysLine.getCreatedBy().getId();
                this.createdByName = sysLine.getCreatedBy().getName();
            }
            if (sysLine.getUpdatedBy() != null) {
                this.updatedById = sysLine.getUpdatedBy().getId();
                this.updatedByName = sysLine.getUpdatedBy().getName();
            }
        }

        public static List<SysLineView> getViewListFromList(List<SysLine> sysLineList) {
            return sysLineList.stream().map(SysLineView::new).toList();
        }
    }

    @Data
    @Schema(title = "线路树表信息", description = "线路树表信息")
    public static class SysLineTreeView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "站点树")
        List<SysLineLocation.SysLineLocationTreeItemView> locationTree;

        public SysLineTreeView(SysLine sysLine, List<SysLineLocation.SysLineLocationTreeItemView> locationTree) {
            this.id = sysLine.getId();
            this.name = sysLine.getName();
            this.locationTree = locationTree;
        }

        public static List<SysLineView> getViewListFromList(List<SysLine> sysLineList) {
            return sysLineList.stream().map(SysLineView::new).toList();
        }
    }

    @Data
    @Schema(title = "线路查询表单", description = "线路查询参数")
    public static class SysLineFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysLine toSysLine() {
            return new SysLine(this);
        }
    }

    @Data
    @Schema(title = "线路保存表单", description = "线路保存参数")
    public static class SysLineSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "名称")
        private String name;

        public SysLine toSysLine() {
            return new SysLine(this);
        }
    }
}
